﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNH_DAL_WS
{
    public class AccountDAO
    {
        public AccountDetailDTO GetAccoutInfo(string username)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from acc in data.ACCOUNTs
                         join role in data.ROLEs on acc.RoleID equals role.ID
                         where acc.Username.Equals(username)
                         select new AccountDetailDTO(acc.ID, acc.Username, acc.FullName, role.Name);

            if (result.Count() > 0)
            {
                AccountDetailDTO acc = new AccountDetailDTO(result.First().ID, result.First().UserName, result.First().FullName, result.First().Role);
                return acc;
            }
            else
                return null;
        }

        public bool IsValidAccount(string username)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var result = from info in data.ACCOUNTs
                         where info.Username.Equals(username)
                         select info;
            if (result.Count() <= 0)
                return false;

            return true;
        }

        public List<RoleDTO> GetAllRole()
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();
            var result = from info in data.ROLEs
                         select info;
            List<RoleDTO> roles = new List<RoleDTO>();
            foreach (ROLE acount in result)
            {
                roles.Add(new RoleDTO(acount.ID, acount.Name));
            }
            return roles;
        }

        public List<AccountDetailDTO> GetAllAccountDetail(string username, string roleID)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();
            var result = from account in data.ACCOUNTs
                         join role in data.ROLEs on account.RoleID equals role.ID
                         where account.Username.IndexOf(username) >= 0 && role.ID.IndexOf(roleID) >= 0
                         select new AccountDetailDTO(account.ID, account.Username, account.FullName, role.Name);

            List<AccountDetailDTO> retValue = new List<AccountDetailDTO>();

            retValue = result.ToList<AccountDetailDTO>();
            return retValue;
        }

        public bool AddAccount(AccountDTO account)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var checkAcc = from acc in data.ACCOUNTs
                           where acc.Username.Equals(account.UserName)
                           select acc;

            if (checkAcc.Count() > 0)
            {
                return false;
            }

            //lay ma so cuoi cung
            var lastID = from acc in data.ACCOUNTs
                         orderby acc.ID descending
                         select acc.ID;
           

            var accCode = (from code in data.CODEs
                           where code.Data.Equals("account")
                           select code).First();

            string accID = accCode.Prefix;

            if (lastID.Count() > 0)
            {
                string tmp = lastID.First();
                accID = Ultilities.GetNextId(tmp, "account");
            }
            else
            {
                for (int i = 0; i < Int32.Parse(accCode.PostfixLength.ToString()); i++)
                {
                    accID += "0";
                }
            }

            account.ID = accID;

            try
            {
                ACCOUNT acc = new ACCOUNT();
                acc.ID = account.ID;
                acc.Username = account.UserName;
                acc.FullName = account.FullName;
                acc.RoleID = account.RoleID;

                data.ACCOUNTs.InsertOnSubmit(acc);
                data.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool EditAccount(AccountDTO account)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var acc = from info in data.ACCOUNTs
                          where info.ID.Equals(account.ID)
                          select info;

            if (acc.Count() > 0)
            {
                acc.First().ID = account.ID;
                acc.First().Username = account.UserName;
                acc.First().FullName = account.FullName;
                acc.First().RoleID = account.RoleID;

                try
                {
                    data.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else
                return false;
        }

        public bool RemoveAccount(string id)
        {
            RestaurantManagementDataContext data = new RestaurantManagementDataContext();

            var acc = from info in data.ACCOUNTs
                       where info.ID.Equals(id)
                       select info;

            if (acc.Count() > 0)
            {
                try
                {
                    data.ACCOUNTs.DeleteOnSubmit(acc.First());
                    data.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }
    }
}